package com.ruoyi.web.controller.chargeback.controller;

import com.ruoyi.charge.bo.AgentAddBo;
import com.ruoyi.charge.bo.AgentEditBo;
import com.ruoyi.charge.bo.AgentQueryBo;
import com.ruoyi.charge.service.IAgentService;
import com.ruoyi.charge.vo.AgentVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 代理商管理Controller
 *
 * @author fanqie
 * @date 2021-10-28
 */
@Api(value = "代理商管理控制器", tags = {"后台代理商管理管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/back/agent")
public class BackAgentController extends BaseController {

    private final IAgentService iAgentService;

    /**
     * 查询代理商管理列表
     */
    @ApiOperation("查询代理商管理列表")
    @PreAuthorize("@ss.hasPermi('back:agent:list')")
    @GetMapping("/list")
    public TableDataInfo<AgentVo> list(AgentQueryBo bo) {
        startPage();
        List<AgentVo> list = iAgentService.queryList(bo);
        return getDataTable(list);
    }

    /**
     * 导出代理商管理列表
     */
    @ApiOperation("导出代理商管理列表")
    @PreAuthorize("@ss.hasPermi('back:agent:export')")
    @Log(title = "代理商管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<AgentVo> export(AgentQueryBo bo) {
        List<AgentVo> list = iAgentService.queryList(bo);
        ExcelUtil<AgentVo> util = new ExcelUtil<AgentVo>(AgentVo.class);
        return util.exportExcel(list, "代理商管理");
    }

    /**
     * 获取代理商管理详细信息
     */
    @ApiOperation("获取代理商管理详细信息")
    @PreAuthorize("@ss.hasPermi('back:agent:query')")
    @GetMapping("/{agentId}")
    public AjaxResult<AgentVo> getInfo(@PathVariable("agentId") Long agentId) {
        return AjaxResult.success(iAgentService.queryById(agentId));
    }

    /**
     * 新增代理商管理
     */
    @ApiOperation("新增代理商管理")
    @PreAuthorize("@ss.hasPermi('back:agent:add')")
    @Log(title = "代理商管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody AgentAddBo bo) {
        if(StringUtils.isNotBlank(bo.getPassword())){
            bo.setPassword(SecurityUtils.encryptPassword(bo.getPassword()));
        }
        return toAjax(iAgentService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改代理商管理
     */
    @ApiOperation("修改代理商管理")
    @PreAuthorize("@ss.hasPermi('back:agent:edit')")
    @Log(title = "代理商管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody AgentEditBo bo) {
        return toAjax(iAgentService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除代理商管理
     */
    @ApiOperation("删除代理商管理")
    @PreAuthorize("@ss.hasPermi('back:agent:remove')")
    @Log(title = "代理商管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{agentIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] agentIds) {
        return toAjax(iAgentService.deleteWithValidByIds(Arrays.asList(agentIds), true) ? 1 : 0);
    }
}
